﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TaxSort
{
    public partial class mainForm : Form
    {
        Thread threadExcelLoader;//Excel导入线程
        DataLoader dataLoader;

        public mainForm()
        {
            InitializeComponent();
            dataLoader = new DataLoader(this,"",dgvData);
            dataLoader.loadDB();
        }

        private void btLoad_Click(object sender, EventArgs e)
        {
            try
            {
                //打开文件
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件夹";
                dialog.Filter = "Excel文件|*.xls;*.xlsx";
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    dgvData.Rows.Clear();
                    string file = dialog.FileName;
                    dataLoader = new DataLoader(this, file, dgvData);
                    threadExcelLoader = new Thread(new ThreadStart(dataLoader.Load));
                    threadExcelLoader.IsBackground = true;
                    threadExcelLoader.Start();
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        
        private void btCompute_Click(object sender, EventArgs e)
        {
            dataLoader = new DataLoader(this, "", dgvData);
            dataLoader.compute(double.Parse(tbLimit.Text));
            dgvData.Rows.Clear();
            dataLoader.loadDB();
            MessageBox.Show("计算完成！");
        }

        private void btLoadPre_Click(object sender, EventArgs e)
        {
            dataLoader = new DataLoader(this, "", dgvData);
            dataLoader.loadDB();
            tsslStatus.Text = "数据加载完成！";
        }

        private void btExport_Click(object sender, EventArgs e)
        {
            dataLoader = new DataLoader(this, "", dgvData);
            dataLoader.save2Excel();
        }
    }
}
